//
//  HZProductSpecView.m
//  app
//
//  Created by 刘华舟 on 15/6/25.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "HZProductSpecView.h"
#import "HZMallHttpManager.h"

static const NSInteger kTagLabelWidth = 86;
static const NSInteger kTagLabelHeight = 60;
static const NSInteger kTypeButtonPadding = 8;
static const NSInteger kTypeButtonWidth = 148;

@interface HZProductSpecView()

@property (strong, nonatomic) UILabel *tagLabel;

@property (strong, nonatomic) NSMutableArray* categoryButtons;
@property (strong, nonatomic) NSArray* categoryDatas;

@property (weak, nonatomic) UIButton* lastButton;

@end

@implementation HZProductSpecView

- (NSMutableArray *)categoryButtons{
    if (_categoryButtons == nil) {
        _categoryButtons = [NSMutableArray array];
    }
    return _categoryButtons;
}

- (void)drawRect:(CGRect)rect{
    [self drawHorizonLineLeft:[self.ubo fw:0] right:[self.ubo fw:0] at:[self.ubo fy:UB_BOTTOM py:-0.5] color:HZ_MALL_M_SEPARATOR_CELL_LINE];
}

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        frame = CGRectOf(frame.origin, [UBO_SCREEN fw:UB_WIDTH uh:kSpecificationsViewHeight]);
        self.backgroundColor = HZ_MALL_MAIN_THEME_WHITE;
        self.contentMode = UIViewContentModeRedraw;
        
        //选择
        self.tagLabel = [[UILabel alloc] initWithFrame:CGRectOf([self.ubo fx:UB_LEFT ux:kSizeMallViewMargin fy:UB_TOP uy:kSizeMallViewMargin],
                                                                [self.ubo uw:kTagLabelWidth uh:kTagLabelHeight])];
        
        
        self.tagLabel.textAlignment = NSTextAlignmentLeft;
        self.tagLabel.textColor = HZ_MALL_TEXT_M_LIGHTGRAY_COLOR;
        self.tagLabel.font = JKAppGlobleFontContentText;
        self.tagLabel.text = @"选择：";
        [self addSubview:self.tagLabel];
    }
    return self;
}


-(void)showCategories:(NSArray*)categories withTitleKey:(NSString *)titleKey default:(NSUInteger)defualtTag
{
    
    self.lastButton = nil;
    
    for(UIButton* cell in self.categoryButtons){
        [cell removeFromSuperview];
    }
    
    [self.categoryButtons removeAllObjects];
    self.categoryDatas = categories;
    if ([categories isEmpty]) {
        return;
    }
    
    NSInteger row = 1;
    
    for(NSInteger i = 0; i<categories.count; i=i+2){
        
        for (NSInteger col = 1; col <= 2; col++) {
            
            if (categories.count < (row -1)*2+col) {
                break;
            }
            
            NSString *title = [[categories objectAtIndex:(row-1)*2+col-1] stringForKey:titleKey nilValue:@""];
            CGFloat totalWidth = [UBO_SCREEN fw:UB_WIDTH uw:-(kTagLabelWidth +3*kSizeMallViewMargin)];
            CGRect titleRect = [title boundingRectWithSize:[UBO_SCREEN fw:UB_WIDTH uw:-(kTypeButtonWidth+kTagLabelWidth +3*kSizeMallViewMargin) ow:-kTypeButtonPadding*2 uh:kTypeButtonHeight] options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:JKAppGlobleFontContentText} context:nil];
            UIButton *btn = nil;
            
            if (col == 1) {
                
                if ((titleRect.size.width+kTypeButtonPadding*2) < [self.ubo uw:kTypeButtonWidth]) {
                    btn = [[UIButton alloc] initWithFrame:CGRectOf([self.ubo fx:UB_LEFT ux:kSizeMallViewMargin ox:self.tagLabel.ufo.xx fy:UB_TOP uy:row*(kTypeButtonHeight)-kTypeButtonHeight+kSizeMallViewMargin oy:kSizeMallViewMargin/2*(row-1)],
                                                                   [self.ubo uw:kTypeButtonWidth uh:kTypeButtonHeight])];
                }else{
                    btn = [[UIButton alloc] initWithFrame:CGRectOf([self.ubo fx:UB_LEFT ux:kSizeMallViewMargin ox:self.tagLabel.ufo.xx fy:UB_TOP uy:row*(kTypeButtonHeight)-kTypeButtonHeight+kSizeMallViewMargin oy:kSizeMallViewMargin/2*(row-1)],
                                                                   [self.ubo ow:(titleRect.size.width+kTypeButtonPadding*2) uh:kTypeButtonHeight])];
                }
            }else{
                UIButton *lastBtn = [self.categoryButtons lastObject];
                
                if ((titleRect.size.width+kTypeButtonPadding*2) > (totalWidth-kTypeButtonPadding*2-lastBtn.ubo.width)) {
                    btn = [[UIButton alloc] initWithFrame:CGRectOf([lastBtn.ufo fx:UB_RIGHT ox:kTypeButtonPadding*2 fy:UB_TOP],
                                                                   [self.ubo ow:(totalWidth-kTypeButtonPadding*2-lastBtn.ubo.width) uh:kTypeButtonHeight])];
                }else{
                    btn = [[UIButton alloc] initWithFrame:CGRectOf([lastBtn.ufo fx:UB_RIGHT ox:kTypeButtonPadding*2 fy:UB_TOP],
                                                                   [self.ubo ow:(titleRect.size.width+kTypeButtonPadding*2) uh:kTypeButtonHeight])];
                }
                
            }
            
            [btn setBackgroundColor:HZ_CLEAR_COLOR];
            
            btn.titleLabel.font = JKAppGlobleFontSmallTagText;
            
            [btn setTitle:title forState:UIControlStateNormal];
            [btn setTitle:title forState:UIControlStateSelected];
            [btn setTitleColor:HZ_MALL_TEXT_S_LIGHTGRAY_COLOR forState:UIControlStateNormal];
            [btn setTitleColor:HZ_WHITE_COLOR forState:UIControlStateSelected];
            [btn setBackgroundColor:HZ_WHITE_COLOR];
            
            btn.layer.masksToBounds = YES;
            btn.layer.cornerRadius = [self.ubo uw:4];
            btn.layer.borderWidth = [self.ubo pw:1];
            btn.layer.borderColor = HZ_MALL_BORDER_LINE_GRAY_COLOR.CGColor;
            
            btn.selected = NO;
            
            btn.tag = (row-1)*2+col-1;
            
            [btn addTarget:self action:@selector(labelDidClicked:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:btn];
            [self.categoryButtons addObject:btn];
            
        }
        
        row++;
        
    }
    
    if (defualtTag > self.categoryButtons.count) {
        defualtTag = 0;
    }
    
    if (self.categoryButtons.count) {
        if (self.lastButton) {
            [self.lastButton setSelected:NO];
            self.lastButton.layer.borderColor = HZ_MALL_BORDER_LINE_GRAY_COLOR.CGColor;
            self.lastButton.backgroundColor = HZ_WHITE_COLOR;
        }
        
        self.lastButton = (UIButton *)[self.categoryButtons objectAtIndex:defualtTag];
        self.lastButton.selected = YES;
        self.lastButton.layer.borderColor = HZ_MALL_MAIN_THEME_ORANGE.CGColor;
        self.lastButton.backgroundColor = HZ_MALL_MAIN_THEME_ORANGE;
    }
    self.backgroundColor = HZ_MALL_MAIN_THEME_WHITE;
}


-(void)labelDidClicked:(UIButton*)btn
{
    if (btn == self.lastButton) {
        return;
    }
    
    if (![[HZMallHttpManager shareHttpRequestMananger] networkReachable]) {
        [MyToast showDefaultBottonWithText:@"请检查网络连接"];
        return;
    }
    
    if (self.lastButton) {
        [self.lastButton setSelected:NO];
        self.lastButton.layer.borderColor = HZ_MALL_BORDER_LINE_GRAY_COLOR.CGColor;
        self.lastButton.backgroundColor = HZ_WHITE_COLOR;
    }
    
    self.lastButton = btn;
    self.lastButton.selected = YES;
    self.lastButton.layer.borderColor = HZ_MALL_MAIN_THEME_ORANGE.CGColor;
    self.lastButton.backgroundColor = HZ_MALL_MAIN_THEME_ORANGE;
    
    if (self.tabSelectedCaller) {
        [self.tabSelectedCaller call:[self.categoryDatas objectAtIndex:btn.tag]];
    }
}


@end
